﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_RecvGoodSheet : DockContent
    {
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.BGDComeBackTab m_bllBGDComeBackTab = new DGBY.BLL.BGDComeBackTab();
     //   private DGBY.BLL.BGDComeBackGoodsMXTab m_bllBGDComeBackGoodsMXTab = new DGBY.BLL.BGDComeBackGoodsMXTab();

        private int m_nSelectBGDComeBackGoodsMXTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        //ExcelBase eB = new ExcelBase();

        public Frm_RecvGoodSheet()
        {
            InitializeComponent();
        }
        private void InitCombox()
        {
            //DataSet ds = m_bllBGDComeBackTab.ShowYiChunPaidanID(@"");
            DataSet ds = m_bllBGDComeBackTab.ShowYiChunPaidanID(@"FactoryDataTab.FactoryName = '东莞市以纯集团有限公司'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = ds.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxContractNo.SelectedIndex = 0;
            }
        }
        private void Frm_RecvGoodSheet_Load(object sender, EventArgs e)
        {
            InitCombox();
        }


        private void ShowAllRecord(string strWhere)
        {

            DataSet ds = m_bllBGDComeBackTab.ShowRecvGoodsInfo(strWhere);


            ultraGrid1.DataSource = null;
            ultraGrid2.DataSource = null;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                //ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
        }

        private void ShowAllMXRecordInDataGridView(int nID)
        {
            DataSet m_ds = m_bllBGDComeBackTab.ShowRecvGoodsMXRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");


            ultraGrid2.DataSource = null;

            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {

                Decimal nNumTotal = 0;

                for (int i = 0; i < m_ds.Tables[0].Rows.Count; i++)
                {

                    nNumTotal += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["数量"].ToString());

                }

                DataRow dr = m_ds.Tables[0].NewRow();

                dr["数量"] = nNumTotal;

                dr["商品名称"] = "合计";
                m_ds.Tables[0].Rows.Add(dr.ItemArray);
                ultraGrid2.DataSource = m_ds.Tables[0];
                ultraGrid2.DisplayLayout.Bands[0].Columns[0].Hidden = true;


            }
        }

        private void ShowInfoByContractNo(string strContractNo)
        {
            int nID = Convert.ToInt32(strContractNo, 10);
            DataSet ds = m_bllBGDComeBackTab.ShowRecvGoodsInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");

            ultraGrid1.DataSource = null;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                this.textBoxSellComp.Text = ds.Tables[0].Rows[0]["公司名称"].ToString();
                this.textBoxCompTel.Text = ds.Tables[0].Rows[0]["公司电话"].ToString();
                this.textBoxFacName.Text = ds.Tables[0].Rows[0]["工厂名称"].ToString();
                this.dateTimePickerDate.Value = (System.DateTime)ds.Tables[0].Rows[0]["日期"];

                ShowAllMXRecordInDataGridView(nID);
            }
        }

        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxContractNo.SelectedValue.ToString();
                
                ShowInfoByContractNo(str);

            }
            catch { }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            //ShowAllRecord(@"");
            ShowAllRecord(@"FactoryDataTab.FactoryName = '东莞市以纯集团有限公司'");
            this.ultraGrid1.Dock = DockStyle.Fill;
            this.groupBoxGoodsDetail.Visible = false;
        }




        private void cellBGDComeBackGoodsMXTabClick()
        {
            //DGBY.Model.BGDComeBackGoodsMXTab modelBGDComeBackGoodsMXTab = m_bllBGDComeBackGoodsMXTab.GetModel(m_nSelectBGDComeBackGoodsMXTabID);
            //if (modelBGDComeBackGoodsMXTab != null)
            //{

            //    this.textBoxGoodsName.Text = modelBGDComeBackGoodsMXTab.GoodsMXName;
            //    this.textBoxMerial.Text = modelBGDComeBackGoodsMXTab.GGXHZDEtc;
            //    this.textBoxGuiGe.Text = modelBGDComeBackGoodsMXTab.GGXHZDEtc2;
            //    this.textBoxNum.Text = modelBGDComeBackGoodsMXTab.FPieceNum.ToString();
            //    this.textBoxNote.Text = modelBGDComeBackGoodsMXTab.RecvGoodsNote;
            //    this.textBoxUnit.Text = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells["单位"].Text;
            //}
        }

 

       
       

        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }



        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
           

            string str = this.comboBoxContractNo.SelectedValue.ToString();
            int nID = Convert.ToInt32(str, 10);
            DataSet ds = m_bllBGDComeBackTab.ShowRecvGoodsInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");
            DataTable dt1 = ds.Tables[0];

            DataTable dt = null;
            DataSet m_ds = m_bllBGDComeBackTab.ShowRecvGoodsMXRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");


            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
            }


            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "收货单.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.RecvGoodsSheet_DataTableToExcel(dt, dt1, 23, 8, 1, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonPrintPreview_Click(object sender, EventArgs e)
        {

            string str = this.comboBoxContractNo.SelectedValue.ToString();
            int nID = Convert.ToInt32(str, 10);
            DataSet ds = m_bllBGDComeBackTab.ShowRecvGoodsInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'"); 
            DataTable dt1 = ds.Tables[0];
            DataTable dt = null;
            DataSet m_ds = m_bllBGDComeBackTab.ShowRecvGoodsMXRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");


            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
            }
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "收货单.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.RecvGoodsSheet_DataTableToExcelAndPrintPreview(dt, dt1, 23, 8, 1, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }


                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {

            string str = this.comboBoxContractNo.SelectedValue.ToString();
            int nID = Convert.ToInt32(str, 10);
            DataSet ds = m_bllBGDComeBackTab.ShowRecvGoodsInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'"); 
            DataTable dt1 = ds.Tables[0];
            DataTable dt = null;
            DataSet m_ds = m_bllBGDComeBackTab.ShowRecvGoodsMXRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");


            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = m_ds.Tables[0];
            }
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "收货单.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.RecvGoodsSheet_DataTableToExcelAndPrint(dt, dt1, 23, 8, 1, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        

        private void Frm_RecvGoodSheet_FormClosing(object sender, FormClosingEventArgs e)
        {
            //eB.Close();
        }

        private void ultraGrid2_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            this.ultraGrid1.Dock = DockStyle.None;
            this.groupBoxGoodsDetail.Visible = true;
            this.ultraGrid1.Dock = DockStyle.None;
            this.groupBoxGoodsDetail.Visible = true;
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            this.comboBoxContractNo.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;

            DataSet ds = m_bllBGDComeBackTab.ShowRecvGoodsInfo("PaidanRegisterTab.ContractCode = '" + this.comboBoxContractNo.Text + "'");


            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                this.textBoxSellComp.Text = ds.Tables[0].Rows[0]["公司名称"].ToString();
                this.textBoxCompTel.Text = ds.Tables[0].Rows[0]["公司电话"].ToString();
                this.textBoxFacName.Text = ds.Tables[0].Rows[0]["工厂名称"].ToString();
                this.dateTimePickerDate.Value = (System.DateTime)ds.Tables[0].Rows[0]["日期"];


            }


        }

        private void ultraGrid2_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid2.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strTemp = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[1].Text;
            if (strTemp == "合计")
                return;
            string strBGDComeBackGoodsMXTabID = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectBGDComeBackGoodsMXTabID = Convert.ToInt32(strBGDComeBackGoodsMXTabID, 10);
            cellBGDComeBackGoodsMXTabClick();

        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    DGBY.Model.BGDComeBackGoodsMXTab modelBGDComeBackGoodsMXTabOld = m_bllBGDComeBackGoodsMXTab.GetModel(m_nSelectBGDComeBackGoodsMXTabID);
            //    if (modelBGDComeBackGoodsMXTabOld == null)
            //    {
            //        MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }

            //    DGBY.Model.BGDComeBackGoodsMXTab modelBGDComeBackGoodsMXTab = new DGBY.Model.BGDComeBackGoodsMXTab();
            //    modelBGDComeBackGoodsMXTab = modelBGDComeBackGoodsMXTabOld;
            //    modelBGDComeBackGoodsMXTab.RecvGoodsNote = this.textBoxNote.Text;

            //    m_bllBGDComeBackGoodsMXTab.Update(modelBGDComeBackGoodsMXTab);
            //    string strContractNo = this.comboBoxContractNo.SelectedValue.ToString();
            //    int nID = Convert.ToInt32(strContractNo, 10);
            //    ShowAllMXRecordInDataGridView(nID);
            //    MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
        }

        private void Frm_RecvGoodSheet_Activated(object sender, EventArgs e)
        {
            InitCombox();
        }

        private void Frm_RecvGoodSheet_Deactivate(object sender, EventArgs e)
        {
            Frm_Main.m_nContractSelectValue = (int)this.comboBoxContractNo.SelectedValue;
        }
    }
}